/**
@author		Jong Min, Lee
@date		  02/10/2014
@breif		study view class

copyright(c) Tae Young Soft. All right reserved. 2014
*/
#include "stdafx.h"
#include "Taurus_Viewer/StudyView.h"
#include "Taurus_Common/Monitor.h"
#include "Taurus_Util/Errors.h"
#include "Taurus_Viewer/MonitorWnd.h"
#include "Taurus_Viewer/StudyViewStyle.h"
#include "Taurus_Viewer/ODSeriesView.h"
#include "Taurus_Viewer/resource.h"

// CStudyView

IMPLEMENT_DYNAMIC(CStudyView, CWnd)

CStudyView::CStudyView()
: m_theMonitorWnd(NULL)
, m_theCurSeriesViewLayout(1,1)
{
}

CStudyView::~CStudyView()
{
}

DWORD CStudyView::attachMonitor(const CMonitorWnd* monitorWnd, 
                                const CRect& workRect, 
                                BOOL redraw)
{
  if(monitorWnd == NULL) return ERROR_INVALID_PARAMETER;

  if(!::IsWindow(this->GetSafeHwnd()))
    return ERROR_INVALID_WINDOW_HANDLE;

  m_theMonitorWnd = monitorWnd;

  ::SetParent(GetSafeHwnd(), monitorWnd->GetSafeHwnd());

  UINT uFlags = SWP_SHOWWINDOW;
  if(!redraw)
    uFlags |= SWP_NOREDRAW;

  SetWindowPos(NULL, workRect.left, workRect.top, workRect.Width(), workRect.Height(), uFlags);

  return ERROR_SUCCESS;
}

const CMonitorWnd* CStudyView::detachMonitor(BOOL redraw)
{
  if(m_theMonitorWnd == NULL)
    return nullptr;

  UINT uFlags = SWP_NOSIZE | SWP_NOMOVE | SWP_HIDEWINDOW;
  if(!redraw)
    uFlags |= SWP_NOREDRAW;

  SetWindowPos(NULL, 0, 0, 0, 0, uFlags);

  const CMonitorWnd* temp = m_theMonitorWnd;
  m_theMonitorWnd = nullptr;
  return temp;
}

void CStudyView::setTitlbarColor(const COLORREF& bkColor, 
                                 const COLORREF& textColor, 
                                 BOOL redraw)
{
  m_theTitlebar.setBkColor(bkColor);
  m_theTitlebar.setTextColor(textColor);

  if(redraw && ::IsWindow(GetSafeHwnd()))
    m_theTitlebar.Invalidate(TRUE);
}

void CStudyView::getTitlebarColor(COLORREF& bkColor, COLORREF& textColor) const
{
  bkColor = m_theTitlebar.getBkColor();
  textColor = m_theTitlebar.getTextColor();
}

void CStudyView::arrangeControls(void)
{
  if(m_theMonitorWnd == NULL) return;

  CRect viewRect;
  GetClientRect(&viewRect);

  MonitorResolutionType resType =  m_theMonitorWnd->getMonitor()->getResolutionType();

  int titlebarHeight = CStudyViewStyle::getTitlebarHeight(resType);
  CRect titlbarRect = viewRect;
  titlbarRect.bottom = titlbarRect.top + titlebarHeight;
  m_theTitlebar.MoveWindow(&titlbarRect, FALSE);

  CRect temp = viewRect;
  viewRect.SubtractRect(temp, titlbarRect);

  double dfUnitHeight = 0.0;
  double dfUnitWidth = 0.0;

  dfUnitHeight = viewRect.Height() / static_cast<double>(m_theCurSeriesViewLayout.theRows);
  dfUnitWidth = viewRect.Width() / static_cast<double>(m_theCurSeriesViewLayout.theCols);

  for(int row = 0; row < m_theCurSeriesViewLayout.theRows; ++row)
  {
    for(int col = 0; col < m_theCurSeriesViewLayout.theCols; ++col)
    {
      CRect rcSeriesView;
      rcSeriesView.left = viewRect.left + round(col * dfUnitWidth);
      rcSeriesView.top = viewRect.top + round(row * dfUnitHeight);
      rcSeriesView.right = rcSeriesView.left + round((col + 1) * dfUnitWidth);
      rcSeriesView.bottom = rcSeriesView.top + round((row + 1) * dfUnitHeight);

      unsigned int index = (row * m_theCurSeriesViewLayout.theCols) + col;
      m_theODSeriesViews[index]->MoveWindow(&rcSeriesView, FALSE);
    }
  }
}

DWORD CStudyView::createODSeriesView(unsigned int index, CODSeriesView*& pSeriesView)
{
  CODSeriesView* outSeriesView = NULL;
  _TRY_ALLOC(outSeriesView, CODSeriesView)
  if(outSeriesView == NULL) return ERROR_NOT_ENOUGH_MEMORY;

  BOOL bCreate = outSeriesView->Create(NULL, NULL, WS_VISIBLE | WS_CHILD, CRect(0,0,0,0), this, IDC_SERIES_VIEW + index, NULL);
  if(!bCreate)
  {
    DWORD dwError = ::GetLastError();
    _DELETE(outSeriesView);
    if(dwError == ERROR_SUCCESS) dwError = ERROR_UNKNOWN;
    return dwError;
  }

  outSeriesView->SetParentStudyView(this);
  pSeriesView = outSeriesView;

  return ERROR_SUCCESS;
}
BEGIN_MESSAGE_MAP(CStudyView, CWnd)
  ON_WM_CREATE()
  ON_WM_SIZE()
  ON_WM_PAINT()
END_MESSAGE_MAP()

// CStudyView message handlers
int CStudyView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
  if(CWnd::OnCreate(lpCreateStruct) == -1)
    return -1;

  BOOL bCreate = m_theTitlebar.Create(NULL, 
                                      NULL, 
                                      WS_CHILD | WS_VISIBLE, 
                                      CRect(0, 0, 0, 0), 
                                      this, 
                                      IDC_STUDY_TITLE_BAR,
                                      NULL);
  if(!bCreate)
    return -1;

  unsigned int nODSeriesViewCount = (unsigned int)(m_theCurSeriesViewLayout.getCount());
  try
  {
    m_theODSeriesViews.reserve(nODSeriesViewCount);
  }
  catch(...)
  {
    return -1;
  }

  DWORD dwError = ERROR_SUCCESS;
  for(unsigned int i = 0; i < nODSeriesViewCount; ++i)
  {
    CODSeriesView* pODSeriesView = NULL;
    dwError = createODSeriesView(i, pODSeriesView);
    if(dwError != ERROR_SUCCESS) break;

    m_theODSeriesViews.push_back(pODSeriesView);
  }

  if(dwError != ERROR_SUCCESS)
  {
    for(unsigned int i = 0; i < m_theODSeriesViews.size(); ++i)
      _DELETE_WND(m_theODSeriesViews[i]);
    m_theODSeriesViews.clear();
    return -1;
  }

  return 0;
}

void CStudyView::OnSize(UINT nType, int cx, int cy)
{
  CWnd::OnSize(nType, cx, cy);

  if(cx > 0 && cy > 0)
    arrangeControls();
}
void CStudyView::OnPaint()
{
  CPaintDC dc(this);

  COLORREF clr = RGB(45, 45, 48);

  CRect rcClient;
  GetClientRect(rcClient);
  dc.FillSolidRect(rcClient, clr);

  CFont font;
  font.CreatePointFont(100, _T("Tahoma"), &dc);
  CFont* oldFont = dc.SelectObject(&font);
  COLORREF oldClr = dc.SetTextColor(RGB(255, 255, 255));
  dc.DrawText(_T("###StudyView###"), -1, &rcClient, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  dc.SetTextColor(oldClr);
  dc.SelectObject(oldFont);
  font.DeleteObject();
}
